﻿using HuiLian.Data.DataAccess.Base;
using HuiLian.Data.DataAccess.Interface;
using HuiLian.Model.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace HuiLian.Data.DataAccess.Implement
{
    public class UserDaoImpl : BaseDaoImpl<User>, IUserDao
    {
        public UserDaoImpl() : base()
        {
            CreateAdminIfNotExist();
        }

        public UserDaoImpl(EFContext context) : base(context)
        {
            CreateAdminIfNotExist();
        }

        private void CreateAdminIfNotExist()
        {
            List<User> list = Context.Users.ToList();
            if (list.Count() > 0)
            {
                return;
            }
            // 默认管理员账号
            User user = new User
            {
                Id = 0,
                Username = "admin",
                Password = "admin",
            };
            Insert(user).Wait();
        }

        public async Task<bool> Login(Expression<Func<User, bool>> where)
        {
            return await Context.Set<User>().Where(where).AnyAsync();
        }
    }
}
